package com.carcontrol.rotate.util;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.Transformation;
import android.widget.FrameLayout;

import com.carcontrol.rotate.MainActivity;


public class Util_Rotate3DAnimation2 extends Animation {
    private final float mFromDegrees;
    private final float mToDegrees;
    private final float mCenterX;
    private final float mCenterY;
    private final float mDepthZ;
    private final boolean zheng;
    private final boolean mReverse;
    private Camera mCamera;
    private FrameLayout frameLayout;
    private int width;
    private float dx;
    private boolean isEnd;
    public static float degrees;
    public static float degrees2;
    public static float wx;
    public static float wx2;
    public static float alpha;
    public static float alpha2;

    public Util_Rotate3DAnimation2(FrameLayout fl, int w, float dx, float fromDegrees, float toDegrees,
                                   float centerX, float centerY, float depthZ, boolean zheng, boolean reverse, boolean isEnd) {
        frameLayout = fl;
        width = w;
        this.dx = dx;
        this.mFromDegrees = fromDegrees;
        this.mToDegrees = toDegrees;
        this.mCenterX = centerX;
        this.mCenterY = centerY;
        this.mDepthZ = depthZ;
        this.zheng = zheng;
        this.mReverse = reverse;
        this.isEnd = isEnd;
    }

    public float getDegrees() {
        return degrees;
    }

    public void setDegrees(float degrees) {
        this.degrees = degrees;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        mCamera = new Camera();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        final float fromDegrees = mFromDegrees;
//        float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);

        float d = isEnd?interpolatedTime:dx;
//        if(dx>0){
//            d = isEnd?interpolatedTime:dx;
//        }else{
//            d = isEnd?interpolatedTime:dx;
//        }

//        d = d<0?-d:d;
        if(mReverse){
            degrees = fromDegrees + ((mToDegrees - fromDegrees) * d);
        }else{
            if(d>0){
                degrees2 = fromDegrees + ((mToDegrees - fromDegrees) * d);
            }else{
                degrees2 = fromDegrees - ((mToDegrees - fromDegrees) * d);
            }

        }


        final float centerX = mCenterX;
        final float centerY = mCenterY;
        final Camera camera = mCamera;



//        Log.e("applyTransformation===", isEnd+"==="+ MainActivity.isStop+"==="+dx+"==="+d+"==="+wx+"==="+wx2+"==="+degrees+"==="+degrees2+">>>"+(-width * (1.0f + d))+"==="+mReverse+"==="+mDepthZ+"==="+interpolatedTime+"==="+fromDegrees+"==="+mToDegrees);

        if(MainActivity.isStop){
            return;
        }


        final Matrix matrix = t.getMatrix();

        camera.save();
        if(dx==0) {
            if(zheng){
                if (mReverse) {
                    camera.translate(width * (1.0f - d), 0.0f, 0);

                    frameLayout.setAlpha(d);
                    camera.rotateY(degrees);
                } else {
                    camera.translate(-width * d, 0.0f, 0);

                    frameLayout.setAlpha(1.0f - d);
                    camera.rotateY(degrees2);
                }
            }else{
                if (mReverse) {
                    camera.translate(width * d, 0.0f, 0);

                    frameLayout.setAlpha(1.0f - d);
                    camera.rotateY(degrees);
                }else{
                    camera.translate(-width * (1.0f - d), 0.0f, 0);

                    frameLayout.setAlpha(d);
                    camera.rotateY(degrees2);
                }
            }
        }else if(dx>0){
            if (mReverse) {
//            camera.translate(-1080 * interpolatedTime, 0.0f, mDepthZ * interpolatedTime);
//            camera.translate(1080 , 0.0f, 0);
//            camera.translate(width * (1.0f - interpolatedTime), 0.0f, 0);
//            if(d>0){
//                camera.translate((isEnd?wx:width) * (1.0f - d), 0.0f, 0);
//            }else{
//                camera.translate(width * d, 0.0f, 0);
//            }

                camera.translate((isEnd?wx:width) * (1.0f - d), 0.0f, 0);

//            camera.translate(width * d, 0.0f, 0);

//            camera.translate(1580 * (1.0f - interpolatedTime), 0.0f, mDepthZ * interpolatedTime);
//            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
//            camera.translate(1080 * (1.0f - interpolatedTime), 1080 * (1.0f - interpolatedTime), mDepthZ * interpolatedTime);
//            camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);

                if(!isEnd){
                    wx = width * (1.0f - d);
                    alpha = d;
                }


                frameLayout.setAlpha(isEnd?(alpha+(1-alpha)*d):d);
                camera.rotateY(degrees);
            } else {
//            camera.translate(0, 0.0f, mDepthZ * (1.0f - interpolatedTime));
//            camera.translate(0.0f, 0.0f, mDepthZ* interpolatedTime);
//            camera.translate(-width * interpolatedTime, 0.0f, mDepthZ* interpolatedTime);
//            camera.translate(-(isEnd?wx2:width) * d, 0.0f, 0);
                camera.translate(-(isEnd?(wx2+(width-wx2)*d):(width * d)), 0.0f, 0);

                if(!isEnd){
                    wx2 = width * d;
                    alpha2 = d;
                }

                frameLayout.setAlpha(1.0f - (isEnd?(alpha2+(1-alpha2)*d):d));
                camera.rotateY(degrees2);
            }
        }else if(dx<0){
            if (mReverse) {
                //            camera.translate(-width * d, 0.0f, 0);
                camera.translate(isEnd?(wx+(width-wx)*d):(-width * d), 0.0f, 0);

                if(!isEnd){
                    wx = -width * d;
                    alpha = -d;
                }

                frameLayout.setAlpha(1.0f - (isEnd?(alpha+(1-alpha)*d):(-d)));
//            frameLayout.setAlpha(1.0f + d);
                camera.rotateY(isEnd?degrees:-degrees);
            }else{
//                camera.translate((isEnd?wx2:width) * (1.0f - d), 0.0f, 0);
//                camera.translate(width*d, 0.0f, 0);
//                camera.translate(width * (1.0f - d), 0.0f, 0);
                camera.translate(isEnd?(wx2-wx2*d):(-width * (1.0f + d)), 0.0f, 0);
//                camera.translate(-width/2, 0.0f, 0);

                Log.d("applyTransformation===4", isEnd+"==="+dx+"==="+d+"==="+wx+"==="+wx2+"==="+degrees+"==="+degrees2+">>>"+(wx2-wx2*d)+"==="+mReverse+"==="+mDepthZ+"==="+interpolatedTime+"==="+centerX+"==="+centerY);


                if(!isEnd){
                    wx2 = -width * (1.0f + d);
                    alpha2 = -d;
                }

//                frameLayout.setAlpha(isEnd?(alpha2+(1-alpha2)*d):d);
                frameLayout.setAlpha(isEnd?(alpha2+(1-alpha2)*d):(-d));
                camera.rotateY(degrees2);
            }

        }


        camera.getMatrix(matrix);
        camera.restore();

        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}
